/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 *
 * @author Danang
 */
public class PostingList {
    private String token;
    // HashMap<Document ID, ArrayList<Posisi Kemunculan>>
    HashMap<Integer, ArrayList<Integer>> position;
    
    public PostingList(String token) {
        this.token = token;
        position = new HashMap<Integer, ArrayList<Integer>>();
    }
    
    public void addPosition(int message_id, ArrayList<Integer> list) {
        position.put(message_id, list);
    }
    
    public int getDF() {
        return position.keySet().size();
    }
    
    public int getTF(int docID) {
        return ((ArrayList<Integer>)position.get(docID)).size();
    }
    
    /**
     * @return the token
     */
    public String getToken() {
        return token;
    }

    /**
     * @param token the token to set
     */
    public void setToken(String token) {
        this.token = token;
    }
    
    public List sortPostingList() {
        List<Entry> tes = new ArrayList<Entry>(position.entrySet());
        Collections.sort (tes, new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                int result = (((Integer)((Map.Entry) o1).getKey()))-(((Integer)((Map.Entry) o2).getKey()));
                return result==0 ? 0 : result > 0 ? 1 : -1;
            }
        });
        return tes;
    }
    
    public String getAllPosition() {
        if (position.isEmpty())
            return "";
        StringBuilder sb = new StringBuilder();
       
        List<Entry> tes = this.sortPostingList();

        int message_id;
        ArrayList<Integer> positionList;
        for (Entry a : tes) {
            message_id = (Integer) a.getKey();
            positionList = (ArrayList<Integer>) a.getValue();
            sb.append(message_id).append(",");
            for (int pos : positionList) {
                sb.append(pos).append(",");
            }
            sb.deleteCharAt(sb.length()-1);
            sb.append("\n");
            positionList = null;
        }
        
        return sb.toString();
    }
    // format output di soal
    public String toString() {
        if (position.isEmpty())
            return "";
        StringBuilder sb = new StringBuilder();
       
        List<Entry> tes = this.sortPostingList();

        int message_id;
        ArrayList<Integer> positionList;
        for (Entry a : tes) {
            message_id = (Integer) a.getKey();
            positionList = (ArrayList<Integer>) a.getValue();
            sb.append("MSG-ID=").append(message_id).append(" TF=").append(positionList.size()).append(" ");
            for (int pos : positionList) {
                sb.append(pos).append(" ");
            }
            sb.deleteCharAt(sb.length()-1);
            sb.append("\n");
            positionList = null;
        }
        
        return sb.toString();
    }

    
    
    
}
